package com.copm.demonstrationsystem.service.introductionfeatures.impl;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.config.SystemConfig;
import com.copm.demonstrationsystem.dao.commons.ImageDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.CompanyIntroductionDao;
import com.copm.demonstrationsystem.dao.introductionfeatures.IntroductionImageDao;
import com.copm.demonstrationsystem.dto.introductionfeatures.CompanyIntroductionDto;
import com.copm.demonstrationsystem.pojo.commons.Image;
import com.copm.demonstrationsystem.pojo.introductionfeatures.CompanyIntroduction;
import com.copm.demonstrationsystem.pojo.introductionfeatures.IntroductionImage;
import com.copm.demonstrationsystem.service.introductionfeatures.CompanyIntroductionService;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.MultiImageAndTextHandle;
import com.copm.demonstrationsystem.service.introductionfeatures.commons.TransformImagePath;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 19:59 2018/4/16
 */
@Service
public class CompanyIntroductionServiceImpl implements CompanyIntroductionService {

    private org.slf4j.Logger logger = LoggerFactory.getLogger(CompanyIntroductionServiceImpl.class);

    @Autowired
    private SystemConfig config;

    @Autowired
    CompanyIntroductionDao companyIntroductionDao;

    @Autowired
    IntroductionImageDao introductionImageDao;

    @Autowired
    MultiImageAndTextHandle multiImageAndTextHandle;

    @Autowired
    TransformImagePath transformImagePath;

    /**
     * 不需要id查找，企业简介只有一张页面可以不需要ID查询
     */
    @Override
    public ResponseStatus getCompanyIntroduction() {
        ResponseStatus status = new ResponseStatus();

        CompanyIntroductionDto dto = new CompanyIntroductionDto();
        CompanyIntroduction companyIntroduction = companyIntroductionDao.findPage();
        //返回的data（Object对象数组），包括CompanyIntroduction && IntroductionImage
        //Object[] obj = new Object[2];
        //通过IntroductionImage获取“企业简介”页面背景图片
        List<IntroductionImage> list = introductionImageDao.findByImageKind(6L);
        for (int i = 0; i < list.size(); i++) {
            IntroductionImage introductionImage = list.get(i);
            Image image = introductionImage.getImage();
            if (null == image) {
                continue;
            }
            transformImagePath.transformImagePath(image);
        }
        if (companyIntroduction == null) {
            status.setStatue(0);
            status.setMessage("查无此页面");
            logger.info("查无此页面");
            return status;
        }

        status.setStatue(1);
        //添加CompanyIntroduction对象
        dto.setContent(companyIntroduction.getIntroduction());
        dto.setTitle(companyIntroduction.getTitle());
        dto.setVideoUrl(companyIntroduction.getVideoUrl());
        //obj[0] = companyIntroduction;
        //添加IntroductionImage对象集合
        //obj[1] = list;
        dto.setImages(list);
        status.setData(dto);
        logger.info("“企业简介”页面信息成功获取");
        return status;
    }

    /**
     * 提供“企业简介”页面 标题修改、添加功能的共同接口
     *
     * @param title
     * @return
     */
    @Override
    public ResponseStatus setCompanyIntroductionTitle(String title) {
        ResponseStatus status = new ResponseStatus();
        //查询“企业简介”页面（只有一个页面数据）
        CompanyIntroduction companyIntroduction = companyIntroductionDao.findPage();
        if (null == companyIntroduction) {
            //新建“企业简介”页面对象
            companyIntroduction = new CompanyIntroduction();
        }
        //设置页面对象
        companyIntroduction.setTitle(title);
        //保存页面对象
        companyIntroductionDao.save(companyIntroduction);
        status.setStatue(1);

        logger.info("“企业简介”页面标题修改、添加成功");
        return status;
    }

    /**
     * 提供“企业简介”页面 文字修改、添加功能的共同接口
     *
     * @param introduction
     * @return
     */
    @Override
    public ResponseStatus setCompanyIntroductionText(String introduction) {
        ResponseStatus status = new ResponseStatus();
        //查询“企业简介”页面（只有一个页面数据）
        CompanyIntroduction companyIntroduction = companyIntroductionDao.findPage();
        if (null == companyIntroduction) {
            companyIntroduction = new CompanyIntroduction();
        }
        //设置页面对象
        companyIntroduction.setIntroduction(introduction);
        //保存页面对象
        companyIntroductionDao.save(companyIntroduction);


        status.setStatue(1);

        logger.info("“企业简介”页面文字修改、添加成功");
        return status;
    }


    /**
     * 提供“企业简介”页面 视频链接地址修改、添加功能的共同接口
     *
     * @param url
     * @return
     */
    @Override
    public ResponseStatus setCompanyIntroductionVideoUrl(String url) {
        ResponseStatus status = new ResponseStatus();
        //查询“企业简介”页面（只有一个页面数据）
        CompanyIntroduction companyIntroduction = companyIntroductionDao.findPage();
        if (null == companyIntroduction) {
            companyIntroduction = new CompanyIntroduction();
        }
        //设置页面对象
        companyIntroduction.setVideoUrl(url);
        //保存页面对象
        companyIntroductionDao.save(companyIntroduction);

        status.setStatue(1);
        logger.info("“企业简介”页面视频链接地址修改、添加成功");
        return status;
    }

    /**
     * “企业简介”页面背景图片添加
     *
     * @param file
     * @return
     */
    @Override
    public ResponseStatus addCompanyIntroductionImage(MultipartFile file) {
        ResponseStatus status = multiImageAndTextHandle.addIntroductionImage(file, 6L);

        logger.info("“企业简介”页面背景图片添加成功");
        return status;
    }

    /**
     * “企业简介”页面背景图片删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseStatus deleteCompanyIntroductionImage(Long id) {
        ResponseStatus status = multiImageAndTextHandle.deleteIntroductionImage(id);
        logger.info("“企业简介”页面背景图片删除成功");
        return status;
    }

//    /**
//     * “企业简介”背景图片修改
//     *
//     * @param id
//     * @param file
//     * @return
//     */
//    @Override
//    public ResponseStatus modifCompanyIntroducitonImage(MultipartFile file, Long id) {
//        //删除待修改图片
//        ResponseStatus status = deleteCompanyIntroductionImage(id);
//        if (status.getStatue() == 0) {
//            return status;
//        }
//        //添加修改图片
//        status = addCompanyIntroductionImage(file);
//        status.setStatue(1);
//        return status;
//    }


}
